package com.atguigu.gmall.index.controller;

import com.atguigu.gmall.common.bean.ResponseVo;
import com.atguigu.gmall.index.service.IndexService;
import com.atguigu.gmall.pms.entity.CategoryEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author: 86182
 * @Date: 2021/8/10
 * @Time: 18:11
 * @Version:
 */
@Controller
public class IndexController {
    @Autowired
    private IndexService indexService;

    @GetMapping("xxx")
    @ResponseBody
    public String test( @RequestHeader("username")Object username){
        return " username = " + username;
    }

    @GetMapping
    public String toIndex(Model model) {
        List<CategoryEntity> categoryEntities = indexService.queryLvl1Categories();
        model.addAttribute("categories",categoryEntities );
        return "index";
    }
    @GetMapping("index/cates/{pid}")
    @ResponseBody
    public ResponseVo<List<CategoryEntity>> queryLvl2WithSubsByPid(@PathVariable("pid") Long pid){
        List<CategoryEntity> categoryEntitys = this.indexService.queryLvl2WithSubsByPid(pid);
        return ResponseVo.ok(categoryEntitys);
    }

//    @GetMapping("index/test/lock")
//    @ResponseBody
//    public ResponseVo testLock(){
//        this.indexService.testLock();
//        return ResponseVo.ok();
//    }

}
